package com.telecom.cms.dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.telecom.cms.dto.CimPage;
import com.telecom.cms.dto.CimPageAtom;
import com.telecom.cms.util.Constant;
import com.telecom.cms.util.CurrentTime;

public class CimPageAtomDAO {
	private static List cimpageatomlist=null;
	Dao dao = new DaoImpStd();

	String db = (new Constant()).getPara("DATABASE");

	public int insert(CimPageAtom transientInstance) {
		int rs = 0;
		String sql = "select * from  " + db + ".Cim_Page_Atom where page_id="
				+ transientInstance.getPageId()+" and biz_atom_id="+transientInstance.getBizAtomId();
		try {
			ResultSet rs2 = dao.select(sql);
			if (rs2.next())
				sql = "update " + db + ".Cim_Page_Atom set Creator_Name='"
						+ transientInstance.getCreatorName()
						+ "',Create_Time=timestamp '"
						+ CurrentTime.getCurrentTime() + "'where page_id="
				        + transientInstance.getPageId()+" and biz_atom_id="+transientInstance.getBizAtomId();
			else
				sql = "insert into " + db + ".Cim_Page_Atom values("
						+ transientInstance.getPageId() + ","
						+ transientInstance.getBizAtomId() + ", '"
						+ transientInstance.getCreatorName() + "' , "
						+ " timestamp '"
						+ CurrentTime.getCurrentTime() + "')";
			rs = dao.insert(sql);
			
			if (rs2 != null) {

				rs2.close();
			}

		} catch (Exception re) {
			System.out.println("save failed:" + re);
		}
		finally
		{
			dao.closeconn();
		}
		return rs;
		
	}

	public void delete(CimPageAtom persistentInstance) {

		String sql = "delete from " + db + ".Cim_Page_Atom where page_id="
				+ persistentInstance.getPageId()+" and biz_atom_id="+persistentInstance.getBizAtomId();

		try {
			dao.delete(sql);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	public void deletebypageid(String pageid) {
		String sql = "delete from " + db + ".Cim_Page_Atom where page_id=" + pageid;
		try {
			dao.delete(sql);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public CimPageAtom findById(String pageId,String bizAtomId) {
		if(cimpageatomlist==null)
			findAll();
		for(int i=0;i<cimpageatomlist.size();i++)
			if(((CimPageAtom)cimpageatomlist.get(i)).getPageId()==Integer.parseInt(pageId)&&((CimPageAtom)cimpageatomlist.get(i)).getBizAtomId()==Integer.parseInt(bizAtomId))
				return (CimPageAtom)cimpageatomlist.get(i);
		return null;

	}

	public List findAll() {

		if(cimpageatomlist!=null)
			return cimpageatomlist;
		
		return findAllForce();

	}
	
	public List findAllForce() {

		String sql = "select * from " + db + ".Cim_Page_Atom  order by biz_atom_id";

		List list = new ArrayList();
		try {
			ResultSet rs = dao.select(sql);
			while (rs.next()) {
				CimPageAtom sd = new CimPageAtom();

				sd.setPageId(rs.getInt(1));
				sd.setBizAtomId(rs.getInt(2));
				sd.setCreatorName(rs.getString(3));
				sd.setCreatTime(rs.getString(4));
				
				list.add(sd);
			}
			if (rs != null) {

				rs.close();
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			dao.closeconn();
		}
		cimpageatomlist=list;
		return list;

	}
	
	public List findAll(String pageId) {
		List list = new ArrayList();
		if(cimpageatomlist==null)
			findAll();
		CimPage sd = new CimPage();
		for(int i=0;i<cimpageatomlist.size();i++)
			if(((CimPageAtom)cimpageatomlist.get(i)).getPageId()==Integer.parseInt(pageId))
				list.add((CimPageAtom)cimpageatomlist.get(i));
		return list;
	}

}